
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

/*
 * CoordinationGame.java
 *
 * Created on Feb 25, 2009, 9:47:24 AM
 */

/**
 *
 * @author Vinay
 */
public class CoordinationGame extends javax.swing.JFrame {

    int payoffMat[][] = new int[2][2];
    int choice1, choice2;
    boolean finished=false;
    Game g;
    String desc="";

    /** Creates new form CoordinationGame */
    public CoordinationGame() {
        initComponents();
        g = new Game();
        this.addMouseListener(new CoordinationGame.CheckMouse());
    }

    public CoordinationGame(int payoff[][])
    {
        this.payoffMat = payoff;
        desc = "<html>If you both select A, you get $"+payoff[0][0];
        desc+="<br>If you select A and player selects B, you get $"+payoff[0][1];
        desc+="<br>If you select B and player selects A, you get $"+payoff[1][0];
        desc+="<br>If you both select B, you get $"+payoff[1][1]+"</html>";
        System.out.println("Desc : "+desc);
        initComponents();
        lblDesc.setText(desc);
        btnNext.setVisible(false);
        g = new Game();
        this.addMouseListener(new CoordinationGame.CheckMouse());
    }

    class CheckMouse implements MouseListener{
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {
            int modifiers = e.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                if(btnNext.isVisible()) {
                    setVisible(false);
                    g.free = true;
                }
                else {
                    chooseA();
                }
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {}
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                if(btnNext.isVisible()) {
                    setVisible(false);
                    g.free = true;
                }
                else {
                    chooseB();
                }
            }
        }
    }

    public void getResult()
    {
        String player2choice[] = new String[2];
        Random rand = new Random();
        player2choice[0] = "A";
        player2choice[1] = "B";
        choice2 = rand.nextInt(1)+1;
        lblResult.setText("Player 2 chose '"+player2choice[choice2]+"'.  You got $ "+payoffMat[choice1][choice2]);
        logResult(payoffMat[choice1][choice2]);
    }

    private void logResult(int money)
    {
        String logText = "Coordination, params: (A,A)="+payoffMat[0][0]+" (A,B)="+payoffMat[0][1]+" (B,A)="+payoffMat[1][0]+" (B,B)="+payoffMat[1][1]+", Result : $"+money;
        g.writeLog(logText);
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void chooseA() {
        choice1 = 0;
        System.out.println("Choice1 set to 0");
        getResult();
        btnChoice1.setVisible(false);
        btnChoice2.setVisible(false);
        btnNext.setVisible(true);
        finished = true;
    }
    
    private void chooseB() {
        choice1 = 1;
        System.out.println("Choice1 set to 1");
        getResult();
        btnChoice1.setVisible(false);
        btnChoice2.setVisible(false);
        btnNext.setVisible(true);
        finished = true;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblDesc = new javax.swing.JLabel();
        lblInst = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnChoice1 = new javax.swing.JButton();
        btnChoice2 = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        lblResult = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        lblDesc.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
        lblDesc.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblDesc.setText("Instructions go here");
        lblDesc.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        lblDesc.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        lblInst.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblInst.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblInst.setText("Choose A or B");
        lblInst.setAlignmentY(0.0F);
        lblInst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        btnChoice1.setFont(new java.awt.Font("Tahoma", 0, 14));
        btnChoice1.setText("A");
        btnChoice1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChoice1ActionPerformed(evt);
            }
        });

        btnChoice2.setFont(new java.awt.Font("Tahoma", 0, 14));
        btnChoice2.setText("B");
        btnChoice2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChoice2ActionPerformed(evt);
            }
        });

        btnNext.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnNext.setText("Next");
        btnNext.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(150, 150, 150)
                .addComponent(btnChoice1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(btnChoice2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(139, 139, 139))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnChoice1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnChoice2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        lblResult.setFont(new java.awt.Font("Tahoma", 0, 16));
        lblResult.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblDesc, javax.swing.GroupLayout.PREFERRED_SIZE, 727, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(lblInst, javax.swing.GroupLayout.PREFERRED_SIZE, 730, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(lblResult, javax.swing.GroupLayout.PREFERRED_SIZE, 730, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(lblDesc, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(lblInst, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblResult, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(50, 50, 50)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnChoice1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChoice1ActionPerformed
        // TODO add your handling code here:
        chooseA();
}//GEN-LAST:event_btnChoice1ActionPerformed

    private void btnChoice2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChoice2ActionPerformed
        // TODO add your handling code here:
        chooseB();
    }//GEN-LAST:event_btnChoice2ActionPerformed

    class MyMouseListener implements MouseListener
    {
        public void mouseExited(MouseEvent e){};
        public void mouseEntered(MouseEvent e){};
        public void mouseReleased(MouseEvent e){};
        public void mousePassed(MouseEvent e){};
        public void mousePressed(MouseEvent e){};
        public void mouseClicked(MouseEvent e)
        {
            if(e.getButton()==e.BUTTON1)
            {
                
            }
            else if(e.getButton()== e.BUTTON3)
            {
                
            }
        }
    }

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        g.free=true;
        
    }//GEN-LAST:event_btnNextActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CoordinationGame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChoice1;
    private javax.swing.JButton btnChoice2;
    private javax.swing.JButton btnNext;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblDesc;
    private javax.swing.JLabel lblInst;
    private javax.swing.JLabel lblResult;
    // End of variables declaration//GEN-END:variables

}
